package net.ib.mtalk.network.task;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/**
 * HandlerThread를 갖고 있는 클래스.<br>
 * API Call들의 Thread를 Queueing한다.<br>
 * Application 생명주기에 따라  onCreate()에 이 Class를 호출하게 된다.
 * @author eugenel
 * @see net.ib.mtalk.network.task.GlobalApplication#onCreate
 */
public class NetTaskHandler {

	private static NetTaskHandler taskHandler = null;
	
	private  HandlerThread mHandlerThread;
	private  MyHandler mHandler;
	private  Looper looper;
	
	private NetTaskHandler(){
		mHandlerThread = new HandlerThread("AsyncNetTask");
		mHandlerThread.start();
		looper = mHandlerThread.getLooper();	
		mHandler = new MyHandler(looper);
	}
	
	public static NetTaskHandler getInstance(){
		if(taskHandler==null){
			taskHandler = new NetTaskHandler();
		}
		return taskHandler;
	}
	
	public Handler getHandler(){
		return mHandler;
	}
	
	class MyHandler extends Handler {		
		public MyHandler(Looper looper) {
			super(looper);
		}
		
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			AsyncNetTask task = (AsyncNetTask) msg.obj;
			task.onWorkRequeated(task);
		}
	}
}
